package com.dollama.random.presenter.impl;

import android.util.Log;

import com.dollama.random.model.BookModel;
import com.dollama.random.model.RandomModel;
import com.dollama.random.model.impl.BookModelImpl;
import com.dollama.random.model.impl.SortTestImpl;
import com.dollama.random.presenter.RandomPresenter;
import com.dollama.random.ui.view.RandomView;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by tony on 2017/5/3.
 */

public class RandomPresenterImpl implements RandomPresenter {

    private static final String TAG = "RandomPresenterImpl";
    private RandomView randomView;
    private RandomModel randomModel;
    private BookModel bookModel;

    public RandomPresenterImpl(RandomView randomView){
        this.randomView = randomView;
//        this.randomModel = new RandomModelImpl();
        this.randomModel = new SortTestImpl();
        this.bookModel = new BookModelImpl();
    }


    @Override
    public void showRandom(String str) {
        this.randomView.setRandomResult(str);
    }

    @Override
    public void doRandom() {
        randomModel.createRandomNumber(this);
    }

    @Override
    public void getBookInfo() {
        bookModel.getBookInfo("",randomView);
    }

    @Override
    public void testRxJava() {
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {

            }
        };
        //-----------------------------------------
        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.d(TAG,"completed");
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG,"error");
            }

            @Override
            public void onNext(String s) {
                Log.d(TAG,"string = " + s);
            }
        };
        //-----------------------------------------
        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onStart();
                subscriber.onNext("123");
                subscriber.onCompleted();
            }
        });

        observable = Observable.just("1","2","3","4");
        String[] strs = {"1","2","3"};
        observable = Observable.from(strs);
        //-----------------------------------------

        observable.subscribe(observer);
        observable.subscribe(subscriber);
        //-----------------------------------------
        Action1<String> action1 = new Action1<String>() {
            @Override
            public void call(String s) {
                Log.e(TAG,"abc = " + s);
            }
        };
        Action1<Throwable> action11 = new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.d(TAG,"error = " + throwable.getMessage());
            }
        };
        Action0 action0 = new Action0() {
            @Override
            public void call() {
                Log.d(TAG,"Completed");
            }
        };
        observable.subscribe(action1,action11,action0);
        //-----------------------------------------
        Observable.just(1,2,3,4).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG,"integer = "+ integer);
            }
        });
        //-----------------------------------------


    }
}
